//
//  TTCenterView.m
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/27.
//  Copyright © 2016年 陶路路. All rights reserved.
//

#import "TTCenterView.h"
#import "TTPIictureCollectionView.h"

@interface TTCenterView ()

@property (nonatomic,strong) UILabel * textLable;
@property (nonatomic,strong) TTPIictureCollectionView * collection;
@property (nonatomic,strong) MASConstraint * constraint;

@end
@implementation TTCenterView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setUpUI];
        self.backgroundColor = RANDOMCOLOR;
    }
    
    return self;
}

-(void)setUpUI{
    
    [self addSubview:self.textLable];
    [self addSubview:self.collection];
    [self.textLable mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.top.equalTo(self);
        make.leading.equalTo(self).offset(10);
    }];
    
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textLable.mas_bottom).offset(10);
        
        make.leading.equalTo(@((375 - 250)/2));
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        
       self.constraint = make.bottom.equalTo(self.collection.mas_bottom).offset(10);
        
    }];

}


-(void)setCenterModel:(TTHomeCellModel *)centerModel{
    
    _centerModel = centerModel;

    self.textLable.text = centerModel.cellModel.text;
    
    [self.constraint uninstall];
    
    if (centerModel.cellModel.pictureArray.count <= 0) {
        
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.constraint = make.bottom.equalTo(self.textLable.mas_bottom).offset(10);
            
        }];
        
        self.collection.hidden = YES;
        
    }else{
        
        self.collection.arrayModel = centerModel.cellModel.pictureArray;
        
        [self.constraint uninstall];
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.constraint = make.bottom.equalTo(self.collection.mas_bottom).offset(10);
        }];
        
        self.collection.hidden = NO;
    }
}


#pragma mark 懒加载

-(UILabel *)textLable{
    
    if (_textLable == nil) {
        
        _textLable = [[UILabel alloc]init];
        _textLable.font = [UIFont systemFontOfSize:16];
        _textLable.numberOfLines = 0;
        _textLable.preferredMaxLayoutWidth = SCREENW - 20;
    }
    
    return _textLable;
    
}

-(TTPIictureCollectionView *)collection{
    
    if (_collection == nil) {
        
        _collection = [[TTPIictureCollectionView alloc]initWithFrame:CGRectZero];
    }
    
    return _collection;
}
@end
